package com.ttl.bc.ap.purchaseorder.action;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.ttl.bc.ap.purchaseorder.db.BCPurchaseOrderBO;
import com.ttl.bc.ap.stkrequest.BCStkRequestForm;
import com.ttl.common.util.CommonUtils;

public class SelectPOAction extends DispatchAction {
	
	public ActionForward blank_po(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) 
			throws Exception {
				
		String user 	 = (String)request.getSession().getAttribute("userName");				
		@SuppressWarnings("unused")
		String preDocNo  = (String)request.getSession().getAttribute("preDocNo");				
						
//		String docNo     = CommonUtils.getMaxBCPurchaseOrderDocNo();
		
		request.getSession().setAttribute("userName", user);
//		request.getSession().setAttribute("docNo", docNo);		
		
				
		System.out.println(CommonUtils.getDateTimeForStamp()+" #user:"+user+" #use :SelectPOAction #mode :blank_po");
		
		return mapping.findForward("blank_po");
	}
	
	//ค้นหา PO=Purchase Order
	public ActionForward poFind(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) 
			throws Exception {
		
		String user 	 = (String)request.getSession().getAttribute("userName");
		System.out.print(CommonUtils.getDateTimeForStamp()+" #user:"+user+" #use :SelectPOAction #mode :moreItemsShow ");

//		DynaActionForm pr = (DynaActionForm)form;		
//		String	itemFind = pr.get("itemFind").toString();
//		itemFind = new String(itemFind.getBytes("ISO8859_1"),"UTF-8");		//แปลงให้ค้นหาภาษาไทยได้		

		POFindForm po = (POFindForm)form;
		String itemFind = "";
		if(po.getItemFind()!=null) {
			itemFind = po.getItemFind().trim(); 
			itemFind = new String(itemFind.getBytes("ISO8859_1"),"UTF-8");		//แปลงให้ค้นหาภาษาไทยได้
		}
		
		request.setAttribute("itemFind", itemFind);		
		
		String hql = "FROM BCPurchaseOrderBean WHERE docNo like '%"+itemFind+"%' " +
				" ittl_purchaseCode like '%"+itemFind+"%' "+
				
				" ORDER BY docNo";	
		System.out.println("hql2 = "+hql);
		new BCPurchaseOrderBO().setPage(request, hql);
		
		return mapping.findForward("success_poFind");
	}	
	
	public ActionForward findPOSelected(ActionMapping mapping, ActionForm form, 
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
				
		String user 	 = (String)request.getSession().getAttribute("userName");
		System.out.println(CommonUtils.getDateTimeForStamp()+" #user:"+user+" #use :SelectPOAction #mode :findPOSelected");
				
		BCPurchaseOrderBO bo = new BCPurchaseOrderBO();		
//old	ArrayList resultsStkReq = (ArrayList)bo.find(form, request);		
		BCStkRequestForm reqForm = (BCStkRequestForm)bo.findStkRequestForm(request);
		
//old		BCStkRequestBO bo2 = new BCStkRequestBO();
//old		BCStkRequestForm reqForm = (BCStkRequestForm)bo2.arrayListForm(resultsStkReq);
					
//old		request.getSession().setAttribute("resultsStkReq", resultsStkReq);
															
		String purchaseOrderDocNo = CommonUtils.getMaxBCPurchaseOrderDocNo("PO1-");
		request.setAttribute("reqForm", reqForm);		
		request.setAttribute("purchaseOrderDocNo", purchaseOrderDocNo);
		request.setAttribute("ttl_purchaseOrderDocNo", purchaseOrderDocNo.substring(6));
		request.getSession().setAttribute("userName", user);		
		
		return mapping.findForward("blank_changePO");
												
	}		
	
	public ActionForward edit(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) 
			throws Exception {

		BCPurchaseOrderBO bo = new BCPurchaseOrderBO();
		BCStkRequestForm reqForm = (BCStkRequestForm)bo.findStkRequestForm(request);	
		
		BCPurchaseOrderBO bo2 = new BCPurchaseOrderBO();
		BCPurchaseOrderForm poForm = (BCPurchaseOrderForm)bo2.findForm(request);		
				
		request.setAttribute("reqForm", reqForm);		
		request.setAttribute("poForm", poForm);		
				
		return mapping.findForward("blank_edit");
	}	
}
